package com.push.xu.xpush.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.push.xu.xpush.constant.Constant;
import com.push.xu.xpush.util.JsonUtils;
import com.push.xu.xpush.util.PushRepeater;
import com.push.xu.xpush.util.SPUtils;

import cn.jpush.android.api.JPushInterface;

import org.json.JSONException;
import org.json.JSONObject;

public class JPushReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Bundle bundle = intent.getExtras();
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(action)) {
			String token = JPushInterface.EXTRA_REGISTRATION_ID;
			SPUtils.getInstance(Constant.PUSH_MESSAGE_TABLE).put("push_token", token);
			PushRepeater.transmitCommandResult(context, bundle.getString(token), null,
					null);
		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {
			String title = bundle.getString(JPushInterface.EXTRA_TITLE);
			String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
			PushRepeater.transmitMessage(context, message, extra, null);
		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(action)) {
			String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
			String content = bundle.getString(JPushInterface.EXTRA_ALERT);
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			int notifyId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			try {
				PushRepeater.transmitNotification(context, notifyId, title, content, extra,
						JsonUtils.toMap(new JSONObject(extra)));
			} catch (JSONException localJSONException2) {
				localJSONException2.printStackTrace();
				PushRepeater.transmitNotification(context, notifyId, title, content, extra, null);
			}
		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
			String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
			String content = bundle.getString(JPushInterface.EXTRA_ALERT);
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			int notifyId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			try {
				PushRepeater.transmitNotificationClick(context, notifyId, title, content, extra,
						JsonUtils.toMap(new JSONObject(extra)));
			} catch (JSONException localJSONException1) {
				localJSONException1.printStackTrace();
				PushRepeater.transmitNotificationClick(context, notifyId, title, content, extra, null);
			}
		} else {
		}
	}
}
